using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Database;

namespace WindowsUserControl.Database
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DatabaseBrowser : Form
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        public delegate void DatabaseSelected(string connStr);

        /// <summary>
        /// 
        /// </summary>
        public event DatabaseSelected DatabaseSelectedEvent;

        /// <summary>
        /// 
        /// </summary>
        public DatabaseBrowser()
        {
            InitializeComponent();
        }

        private void comboBoxServer_DropDown(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (this.comboBoxServer.Items.Count == 0)
                {
                    string[] servers = SqlServerEnumUtil.GetAvailableServers();
                    if (servers != null && servers.Length > 0)
                    {
                        foreach (string server in servers)
                        {
                            comboBoxServer.Items.Add(server);
                        }
                        int index = comboBoxServer.FindStringExact("(local)");
                        if (index >= 0)
                            this.comboBoxServer.SelectedIndex = index;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void comboBoxDatabase_DropDown(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                string server, userName, password;
                bool integratedSecurity;
                this.comboBoxDatabase.Items.Clear();
                server = this.comboBoxServer.Text;
                userName = this.textBoxUserName.Text;
                password = this.textBoxPassword.Text;
                integratedSecurity = this.radioButtonWindowsAuthentication.Checked;
                string[] databases = SqlServerEnumUtil.GetDatabases(server, integratedSecurity, userName, password);
                foreach (string database in databases)
                {
                    this.comboBoxDatabase.Items.Add(database);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void radioButtonWindowsAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            this.Synchronize();
        }

        private void radioButtonSQLServerAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            this.Synchronize();
        }

        private void Synchronize()
        {
            bool windowsAuth = this.radioButtonWindowsAuthentication.Checked;
            this.textBoxUserName.Enabled = !windowsAuth;
            this.textBoxPassword.Enabled = !windowsAuth;
            this.labelUserName.Enabled = !windowsAuth;
            this.labelPassword.Enabled = !windowsAuth;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            string server = this.comboBoxServer.Text;
            string db = this.comboBoxDatabase.Text;
            string uid = this.textBoxUserName.Text;
            string pwd = this.textBoxPassword.Text;
            AuthenticationType authType = AuthenticationType.WindowsAuthentication;
            if (this.radioButtonSQLServerAuthentication.Checked)
                authType = AuthenticationType.SQLServerAuthentication;
            string connStr = DbConnUtil.BuildConnectionString(server, string.Empty, db, authType, uid, pwd);
            if (this.DatabaseSelectedEvent != null)
            {
                this.DatabaseSelectedEvent(connStr);
            }
            this.Dispose();
        }
    }
}